﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Web.Models;
using KL.Store.Data;
using KL.Store.Core;
using KL.Store.Core.Domain;
using System.Web.Caching;

namespace KL.Store.Web.Controllers
{
    public class ProductsController : KL.Store.Core.Web.BaseController
    {
        #region Index

        public ActionResult Index(int? id, decimal? minPrice, decimal? maxPrice, string goNewUrl = "")
        {
            ViewBag.hasSub = Parameters.ShowCategoryOnProductsPages ? "" : "true";
            ViewBag.Controller = "products_crtl";
            if (id.HasValue)
            {
                CategoryMeta cm = dbCategory.GetMeta((int)id);
                Category cd = dbCategory.Get((int)id);

                if (!string.IsNullOrEmpty(goNewUrl) && cd.LinkUrl.Contains(goNewUrl))
                {
                    return RedirectPermanent(cd.LinkUrl);
                }

                if (cm != null && cd != null)
                {
                    ViewBag.Title = string.IsNullOrEmpty(cm.WebTitle) ? cm.CategoryName : cm.WebTitle;
                    ViewBag.Keywords = string.IsNullOrEmpty(cm.Keywords) ? Parameters.WebSiteTitle + " " + cm.CategoryName : cm.Keywords;
                    ViewBag.Description = string.IsNullOrEmpty(cm.Description) ? Parameters.WebSiteTitle + " " + cm.CategoryName : cm.Description;
                    var websiteUrl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;
                    ViewBag.CanonicalUrl = websiteUrl + "/urunler" + cm.Canonical + "-" + cm.CategoryId + ".html";
                    ViewBag.ColumnCount = cm.ColumnCount;
                    ViewBag.ListImageType = cm.ListImageType;
                    ViewBag.SuggestedGroup = cm.SuggestedGroup;
                    ViewBag.ShowcaseGroup = cm.ShowcaseGroup;
                    ViewBag.IsManager = eStoreUser.IsManager;
                    ViewBag.IsSeoManager = eStoreUser.IsSeoManager;
                    eStoreUser.RedirectUrl = cd.LinkUrl;
                    ViewBag.MinPrice = !string.IsNullOrEmpty(minPrice.ToString()) ? minPrice.ToString() : "0";
                    ViewBag.MaxPrice = !string.IsNullOrEmpty(maxPrice.ToString()) ? maxPrice.ToString() : "0";
                }
                else
                {
                    return Redirect("/Content/404");
                }
            }
            else
            {
                return Redirect("/Content/404");
            }
            return View();
        }

        #endregion

        #region BestSellerIndex

        public ActionResult BestSellerIndex()
        {
            ViewBag.Controller = "products_crtl";
            CategoryMeta cm = dbCategory.GetMeta(Parameters.BestSellingCategoryId);
            if (cm != null)
            {
                ViewBag.Title = "Çok Satanlar";
                ViewBag.Keywords = cm.Keywords ?? "";
                ViewBag.Description = cm.Description ?? "";
                ViewBag.ColumnCount = cm.ColumnCount;
                ViewBag.ListImageType = cm.ListImageType;
            }
            var websiteUrl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;
            ViewBag.CanonicalUrl = websiteUrl + "/BestSeller.html";
            eStoreUser.RedirectUrl = "/BestSeller.html";
            return View();
        }

        #endregion

        #region NewProductsIndex

        public ActionResult NewProductsIndex()
        {
            ViewBag.Controller = "products_crtl";
            ViewBag.Title = "En Yeni Ürünler";
            ViewBag.Description = Parameters.SiteMetaDescription;
            ViewBag.ColumnCount = 4;
            ViewBag.ListImageType = "_w";
            eStoreUser.RedirectUrl = "/NewProducts.html";
            var websiteUrl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;
            ViewBag.CanonicalUrl = websiteUrl + "/NewProducts.html";
            return View();
        }

        #endregion

        #region CampaignsIndex

        public ActionResult CampaignsIndex()
        {
            ViewBag.Controller = "products_crtl";
            ViewBag.Title = "Kampanyalar";
            ViewBag.Description = Parameters.SiteMetaDescription;
            ViewBag.ColumnCount = 4;
            ViewBag.ListImageType = "_w";
            eStoreUser.RedirectUrl = "/Campaigns.html";
            var websiteUrl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;
            ViewBag.CanonicalUrl = websiteUrl + "/Campaigns.html";
            return View();
        }

        #endregion

        #region PackagesIndex

        public ActionResult PackagesIndex(int? id, string goNewUrl = "")
        {
            ViewBag.Controller = "products_crtl";
            if (id.HasValue)
            {
                CategoryMeta cm = dbCategory.GetMeta((int)id);
                Category cd = dbCategory.Get((int)id);

                if (!string.IsNullOrEmpty(goNewUrl) && cd.LinkUrl.Contains(goNewUrl))
                {
                    return RedirectPermanent(cd.LinkUrl);
                }

                if (cm != null && cd != null)
                {
                    ViewBag.Title = string.IsNullOrEmpty(cm.WebTitle) ? cm.CategoryName : cm.WebTitle;
                    ViewBag.Keywords = string.IsNullOrEmpty(cm.Keywords) ? Parameters.WebSiteTitle + " " + cm.CategoryName : cm.Keywords;
                    ViewBag.Description = string.IsNullOrEmpty(cm.Description) ? Parameters.WebSiteTitle + " " + cm.CategoryName : cm.Description;
                    var websiteUrl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;
                    ViewBag.CanonicalUrl = websiteUrl + "/paket" + cm.Canonical + "-" + cm.CategoryId + ".html";
                    ViewBag.ColumnCount = cm.ColumnCount;
                    ViewBag.ListImageType = cm.ListImageType;
                    ViewBag.SuggestedGroup = cm.SuggestedGroup;
                    ViewBag.ShowcaseGroup = cm.ShowcaseGroup;
                    ViewBag.IsManager = eStoreUser.IsManager;
                    ViewBag.IsSeoManager = eStoreUser.IsSeoManager;
                    eStoreUser.RedirectUrl = cd.LinkUrl.Replace("urunler", "paket");
                }
                else
                {
                    return Redirect("/Content/404");
                }
            }
            else
            {
                return Redirect("/Content/404");
            }
            return View();
        }

        #endregion

        #region Get

        [Child]
        public ActionResult Get(int? id, int? brand, int? sort, int? order, int? itemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string multipleProperties, string search, int page = 1, int ColumnCount = 4, string ListImageType = "s", int ShowcaseGroup = 0)
        {
            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;
            ViewBag.Page = page;
            var cacheKey = "ProductsControllerGet" + id + brand + sort + order + itemCount + minPrice + maxPrice + multipleBrands + multipleProperties + search + page + ColumnCount + ListImageType + ShowcaseGroup;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }
            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                if (!sort.HasValue) sort = 1;
                if (!order.HasValue) order = 1;
                productsModel.Sort = (int)sort;
                productsModel.Order = (int)order;
                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;
                productsModel.ColumnCount = ColumnCount;
                productsModel.ListImageType = ListImageType;
                productsModel.IsManager = eStoreUser.IsManager;
                productsModel.IsSeoManager = eStoreUser.IsSeoManager;
                productsModel.BasketLocked = eStoreUser.BasketLocked;
                productsModel.ShowcaseGroup = ShowcaseGroup;
                if (!string.IsNullOrEmpty(ListImageType))
                {
                    ViewBag.ListImageType = "_" + ListImageType;
                }
                else
                {
                    ViewBag.ListImageType = "";
                }
                var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
                if (posListe != null)
                {
                    posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                    var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                    var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                    productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                    productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
                }
                if (!sort.HasValue)
                {
                    productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").ToArray<Product>();
                }
                else
                {
                    if (productsModel.Sort == 1)
                    {
                        //İsime Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductName).ThenByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductName).ThenByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 2)
                    {
                        //Fiyata Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.SpecialPrice).ThenByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SpecialPrice).ThenByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 3)
                    {
                        //Yeni-Eski
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductId).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductId).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 4)
                    {
                        //Satışa Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 5)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenBy(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 6)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();

                        }
                    }
                    else if (productsModel.Sort == 7)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                }
                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));

                }

                #region Marka Filtreleme
                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    productsModel.SelectedBrands = multipleBrands.Split('-').ToList();

                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => productsModel.SelectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();
                    productsModel.FilterEnabled = true;
                }
                #endregion

                #region Özellik Filtreleme
                if (!string.IsNullOrEmpty(multipleProperties))
                {
                    productsModel.FilterEnabled = true;
                    var selectedProperties = multipleProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).ToList();
                    selectedProperties.Remove("");
                    //new_product_1-_-1_-_
                    #region Special Filters
                    if (selectedProperties.Contains("new_product_1-_-1"))
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.NewProduct).ToList();
                        selectedProperties.Remove("new_product_1-_-1");
                    }
                    if (selectedProperties.Contains("new_product_0-_-1"))
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => !o.NewProduct).ToList();
                        selectedProperties.Remove("new_product_0-_-1");
                    }
                    if (selectedProperties.Contains("fast_cargo_1-_-1"))
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.FastCargo).ToList();
                        selectedProperties.Remove("fast_cargo_1-_-1");
                    }
                    if (selectedProperties.Contains("fast_cargo_0-_-1"))
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => !o.FastCargo).ToList();
                        selectedProperties.Remove("fast_cargo_0-_-1");
                    }
                    if (selectedProperties.Contains("campaign_1-_-1"))
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.CampaignProduct || o.ShowAsCampaignProduct || o.OpportunityProduct || o.DiscountDisplayTotal > 0 || o.DiscountPercent > 0 || o.DiscountTotal > 0 || !string.IsNullOrEmpty(o.GiftString)).ToList();
                        selectedProperties.Remove("campaign_1-_-1");
                    }
                    if (selectedProperties.Contains("campaign_0-_-1"))
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => !o.CampaignProduct && !o.ShowAsCampaignProduct && !o.OpportunityProduct && o.DiscountDisplayTotal <= 0 && o.DiscountPercent <= 0 && o.DiscountTotal <= 0 && string.IsNullOrEmpty(o.GiftString)).ToList();
                        selectedProperties.Remove("campaign_0-_-1");
                    }


                    #endregion

                    foreach (var property in selectedProperties)
                    {
                        productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.ProductProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).Contains(property)).ToList<Product>();
                    }
                    IEnumerable<int> PropertiesIDs = productsModel.RelatedProducts.Select(o => o.ProductId);
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => PropertiesIDs.Contains(o.ProductId) || PropertiesIDs.Contains(o.UstUrunId)).ToList<Product>();
                }
                #endregion

                if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                {
                    if (minPrice == maxPrice)
                        maxPrice++;
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).ToArray<Product>();
                    productsModel.FilterEnabled = true;
                }
                int relatedProductCount = productsModel.RelatedProducts.Count();

                //sayfalama için
                productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();

                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }
                productsModel.RelatedItemCount = relatedProductCount;
                productsModel.RelatedItemShowCount = category.RelatedItemShowCount;
                if (productsModel.RelatedItemShowCount > 0)
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Take(productsModel.RelatedItemShowCount).ToArray<Product>();
                }
                productsModel.CategoryType = category.CategoryType;

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, Parameters.HomePageCategoryId, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region Get2

        [Child]
        public ActionResult Get2(int? id, int? brand, int? sort, int? order, int? itemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string search, int page = 1, int ColumnCount = 4, string ListImageType = "s", bool lazyLoad = false, bool showTextTitle = true, bool smallMode = true, bool showcase = false)
        {
            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;
            ViewBag.Page = page;
            ViewBag.LazyLoad = lazyLoad;
            ViewBag.ShowTextTitle = showTextTitle;
            ViewBag.SmallMode = smallMode;
            ViewBag.ShowCase = showcase;
            var cacheKey = "ProductsControllerGet2" + id + brand + sort + order + itemCount + minPrice + maxPrice + multipleBrands + search + page + ColumnCount + ListImageType + lazyLoad + showTextTitle + smallMode + showcase;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }

            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                if (!sort.HasValue) sort = 1;
                if (!order.HasValue) order = 1;
                productsModel.Sort = (int)sort;
                productsModel.Order = (int)order;
                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;
                productsModel.ColumnCount = ColumnCount;
                productsModel.ListImageType = ListImageType;
                productsModel.IsManager = eStoreUser.IsManager;
                productsModel.IsSeoManager = eStoreUser.IsSeoManager;
                productsModel.BasketLocked = eStoreUser.BasketLocked;

                var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
                if (posListe != null)
                {
                    posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                    var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                    var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                    productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                    productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
                }
                if (!sort.HasValue)
                {
                    productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").ToArray<Product>();
                }
                else
                {
                    if (productsModel.Sort == 1)
                    {
                        //İsime Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductName).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductName).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 2)
                    {
                        //Fiyata Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.SpecialPrice).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SpecialPrice).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 3)
                    {
                        //Yeni-Eski
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductId).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductId).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 4)
                    {
                        //Satışa Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 5)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenBy(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 6)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 7)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                }
                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));
                }


                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                //if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                //{
                //    if (minPrice == maxPrice)
                //        maxPrice++;
                //    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).ToArray<Product>();
                //}
                int relatedProductCount = productsModel.RelatedProducts.Count();

                //sayfalama için
                productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();

                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }
                productsModel.RelatedItemCount = relatedProductCount;
                productsModel.RelatedItemShowCount = category.RelatedItemShowCount;
                if (productsModel.RelatedItemShowCount > 0)
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Take(productsModel.RelatedItemShowCount).ToArray<Product>();
                }
                productsModel.CategoryType = category.CategoryType;

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, (int)id, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region PackagesGet

        [Child]
        public ActionResult PackagesGet(int? id, int? brand, int? sort, int? order, int? itemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string multipleProperties, string search, int page = 1, int ColumnCount = 4, string ListImageType = "s", int ShowcaseGroup = 0)
        {
            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;
            ViewBag.Page = page;

            var cacheKey = "PackagesGet" + id + brand + sort + order + itemCount + minPrice + maxPrice + multipleBrands + multipleProperties + search + page + ColumnCount + ListImageType + ShowcaseGroup;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                if (!string.IsNullOrEmpty(ListImageType))
                {
                    ViewBag.ListImageType = "_" + ListImageType;
                }
                else
                {
                    ViewBag.ListImageType = "";
                }
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }


            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                if (!sort.HasValue) sort = 1;
                if (!order.HasValue) order = 1;
                productsModel.Sort = (int)sort;
                productsModel.Order = (int)order;
                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;
                productsModel.ColumnCount = ColumnCount;
                productsModel.ListImageType = ListImageType;
                productsModel.IsManager = eStoreUser.IsManager;
                productsModel.IsSeoManager = eStoreUser.IsSeoManager;
                productsModel.BasketLocked = eStoreUser.BasketLocked;
                productsModel.ShowcaseGroup = ShowcaseGroup;
                if (!string.IsNullOrEmpty(ListImageType))
                {
                    ViewBag.ListImageType = "_" + ListImageType;
                }
                else
                {
                    ViewBag.ListImageType = "";
                }
                var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
                if (posListe != null)
                {
                    posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                    var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                    var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                    productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                    productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
                }
                if (!sort.HasValue)
                {
                    productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").ToArray<Product>();
                }
                else
                {
                    if (productsModel.Sort == 1)
                    {
                        //İsime Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductName).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductName).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 2)
                    {
                        //Fiyata Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.SpecialPrice).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SpecialPrice).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 3)
                    {
                        //Yeni-Eski
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductId).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductId).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 4)
                    {
                        //Satışa Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 5)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenBy(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 6)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();

                        }
                    }
                    else if (productsModel.Sort == 7)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                }
                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));

                }

                #region Marka Filtreleme
                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                #endregion

                #region Özellik Filtreleme
                if (!string.IsNullOrEmpty(multipleProperties))
                {
                    var selectedProperties = multipleProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).ToList();
                    selectedProperties.Remove("");
                    IList<Product> PropertiesAvailable = productsModel.RelatedProducts.Where(o => o.ProductProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).Any(x => selectedProperties.Any(y => x.Contains(y)))).ToList<Product>();
                    IEnumerable<int> PropertiesIDs = PropertiesAvailable.Select(o => o.ProductId);

                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => PropertiesIDs.Contains(o.ProductId) || PropertiesIDs.Contains(o.UstUrunId)).ToList<Product>();
                }
                #endregion

                if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                {
                    if (minPrice == maxPrice)
                        maxPrice++;
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).ToArray<Product>();
                }
                int relatedProductCount = productsModel.RelatedProducts.Count();

                //sayfalama için
                productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();

                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }
                productsModel.RelatedItemCount = relatedProductCount;
                productsModel.RelatedItemShowCount = category.RelatedItemShowCount;
                if (productsModel.RelatedItemShowCount > 0)
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Take(productsModel.RelatedItemShowCount).ToArray<Product>();
                }
                productsModel.CategoryType = category.CategoryType;

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, Parameters.HomePageCategoryId, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region Tabber

        public ActionResult Tabber(int? id, int? brand, int? sort, int? order, int? itemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string search, int page = 1, int ColumnCount = 4, string ListImageType = "s", bool lazyLoad = false, bool showTextTitle = true, bool smallMode = true)
        {
            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;
            ViewBag.Page = page;
            ViewBag.LazyLoad = lazyLoad;
            ViewBag.ShowTextTitle = showTextTitle;
            ViewBag.SmallMode = smallMode;

            var cacheKey = "Tabber" + id + brand + sort + order + itemCount + minPrice + maxPrice + multipleBrands + search + page + ColumnCount + ListImageType + lazyLoad + showTextTitle + smallMode;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }

            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                if (!sort.HasValue) sort = 1;
                if (!order.HasValue) order = 1;
                productsModel.Sort = (int)sort;
                productsModel.Order = (int)order;
                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;
                productsModel.ColumnCount = ColumnCount;
                productsModel.ListImageType = ListImageType;
                productsModel.IsManager = eStoreUser.IsManager;
                productsModel.IsSeoManager = eStoreUser.IsSeoManager;
                productsModel.BasketLocked = eStoreUser.BasketLocked;

                var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
                if (posListe != null)
                {
                    posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                    var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                    var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                    productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                    productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
                }
                if (!sort.HasValue)
                {
                    productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").ToArray<Product>();
                }
                else
                {
                    if (productsModel.Sort == 1)
                    {
                        //İsime Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductName).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductName).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 2)
                    {
                        //Fiyata Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.SpecialPrice).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SpecialPrice).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 3)
                    {
                        //Yeni-Eski
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductId).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductId).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 4)
                    {
                        //Satışa Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 5)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenBy(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 6)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 7)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                }
                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));
                }


                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                //if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                //{
                //    if (minPrice == maxPrice)
                //        maxPrice++;
                //    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).ToArray<Product>();
                //}
                int relatedProductCount = productsModel.RelatedProducts.Count();

                //sayfalama için
                productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();

                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }
                productsModel.RelatedItemCount = relatedProductCount;
                productsModel.RelatedItemShowCount = category.RelatedItemShowCount;
                if (productsModel.RelatedItemShowCount > 0)
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Take(productsModel.RelatedItemShowCount).ToArray<Product>();
                }
                productsModel.CategoryType = category.CategoryType;

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, (int)id, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region Full

        #region IndexFull

        public ActionResult IndexFull(int? id, string goNewUrl = "")
        {
            ViewBag.Controller = "products_crtl";
            if (id.HasValue)
            {
                CategoryMeta cm = dbCategory.GetMeta((int)id);
                Category cd = dbCategory.Get((int)id);

                if (!string.IsNullOrEmpty(goNewUrl) && cd.LinkUrl.Contains(goNewUrl))
                {
                    return RedirectPermanent(cd.LinkUrl);
                }

                if (cm != null && cd != null)
                {
                    ViewBag.Title = string.IsNullOrEmpty(cm.WebTitle) ? cm.CategoryName : cm.WebTitle;
                    ViewBag.Keywords = string.IsNullOrEmpty(cm.Keywords) ? Parameters.WebSiteTitle + " " + cm.CategoryName : cm.Keywords;
                    ViewBag.Description = string.IsNullOrEmpty(cm.Description) ? Parameters.WebSiteTitle + " " + cm.CategoryName : cm.Description;
                    var websiteUrl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;
                    ViewBag.CanonicalUrl = websiteUrl + "/urunler" + cm.Canonical + "-" + cm.CategoryId + ".html";
                    ViewBag.ColumnCount = cm.ColumnCount;
                    ViewBag.ListImageType = cm.ListImageType;
                    ViewBag.SuggestedGroup = cm.SuggestedGroup;
                    ViewBag.ShowcaseGroup = cm.ShowcaseGroup;
                    ViewBag.IsManager = eStoreUser.IsManager;
                    ViewBag.IsSeoManager = eStoreUser.IsSeoManager;
                    eStoreUser.RedirectUrl = cd.LinkUrl;
                }
                else
                {
                    return Redirect("/Content/404");
                }
            }
            else
            {
                return Redirect("/Content/404");
            }
            return View();
        }

        #endregion

        #region GetFull

        [Child]
        public ActionResult GetFull(int? id, int? brand, int? sort, int? order, int? itemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string multipleProperties, string search, int page = 1, int ColumnCount = 4, string ListImageType = "s", int ShowcaseGroup = 0)
        {
            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;
            ViewBag.Page = page;
            
            var cacheKey = "GetFull" + id + brand + sort + order + itemCount + minPrice + maxPrice + multipleBrands + multipleProperties + search + page + ColumnCount + ListImageType + ShowcaseGroup;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }

            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                if (!sort.HasValue) sort = 1;
                if (!order.HasValue) order = 1;
                productsModel.Sort = (int)sort;
                productsModel.Order = (int)order;
                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;
                productsModel.ColumnCount = ColumnCount;
                productsModel.ListImageType = ListImageType;
                productsModel.IsManager = eStoreUser.IsManager;
                productsModel.IsSeoManager = eStoreUser.IsSeoManager;
                productsModel.BasketLocked = eStoreUser.BasketLocked;
                productsModel.ShowcaseGroup = ShowcaseGroup;
                if (!string.IsNullOrEmpty(ListImageType))
                {
                    ViewBag.ListImageType = "_" + ListImageType;
                }
                else
                {
                    ViewBag.ListImageType = "";
                }
                var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
                if (posListe != null)
                {
                    posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                    var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                    var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                    productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                    productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
                }
                if (!sort.HasValue)
                {
                    productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").ToArray<Product>();
                }
                else
                {
                    if (productsModel.Sort == 1)
                    {
                        //İsime Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductName).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductName).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 2)
                    {
                        //Fiyata Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.SpecialPrice).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SpecialPrice).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 3)
                    {
                        //Yeni-Eski
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.ProductId).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.ProductId).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 4)
                    {
                        //Satışa Göre
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetList((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenByDescending(o => o.CreatedDate).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 5)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.BillOut).ThenBy(o => o.TotalViewCount).ToArray<Product>();
                        }
                    }
                    else if (productsModel.Sort == 6)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();

                        }
                    }
                    else if (productsModel.Sort == 7)
                    {
                        if (productsModel.Order > 1)
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenByDescending(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                        else
                        {
                            productsModel.RelatedProducts = dbProduct.GetListFull((int)id, 0, (int)brand, eStoreUser.AccountId, "").OrderBy(o => o.SiraNo == 0).ThenBy(o => o.SiraNo).OrderBy(o => o.ProductName).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.BillOut).ToArray<Product>();
                        }
                    }
                }
                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Parameters.ShowPricesWithoutTax ? Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.PriceWithOutTax)) : Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));

                }

                #region Marka Filtreleme
                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                #endregion

                #region Özellik Filtreleme
                if (!string.IsNullOrEmpty(multipleProperties))
                {
                    var selectedProperties = multipleProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).ToList();
                    selectedProperties.Remove("");
                    IList<Product> PropertiesAvailable = productsModel.RelatedProducts.Where(o => o.ProductProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).Any(x => selectedProperties.Any(y => x.Contains(y)))).ToList<Product>();
                    IEnumerable<int> PropertiesIDs = PropertiesAvailable.Select(o => o.ProductId);

                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => PropertiesIDs.Contains(o.ProductId) || PropertiesIDs.Contains(o.UstUrunId)).ToList<Product>();
                }
                #endregion

                if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                {
                    if (minPrice == maxPrice)
                        maxPrice++;
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).ToArray<Product>();
                }
                int relatedProductCount = productsModel.RelatedProducts.Count();

                //sayfalama için
                productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();

                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }
                productsModel.RelatedItemCount = relatedProductCount;
                productsModel.RelatedItemShowCount = category.RelatedItemShowCount;
                if (productsModel.RelatedItemShowCount > 0)
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Take(productsModel.RelatedItemShowCount).ToArray<Product>();
                }
                productsModel.CategoryType = category.CategoryType;

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, Parameters.HomePageCategoryId, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #endregion


        #region GetBrands

        [Child]
        public ActionResult GetBrands(int? id)
        {
            BrandsModel brandsModel = new BrandsModel();
            if (id.HasValue)
            {
                brandsModel.CategoryId = (int)id;
                brandsModel.Brands = dbProduct.GetBrandList((int)id, 0, "").Where(b => b.ItemCount > 0).ToList<Brand>();

                brandsModel.BreadCrumb = "";

                //brandsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, eStoreUser.CultureInfo);
            }

            return PartialView(brandsModel);
        }

        #endregion

        #region GetBrandsSelection

        [Child]
        public ActionResult GetBrandsSelection(int? id, string multipleBrands = "")
        {
            BrandsModel brandsModel = new BrandsModel();
            if (id.HasValue)
            {
                brandsModel.CategoryId = (int)id;
                brandsModel.Brands = dbProduct.GetBrandList((int)id, 0, "").Where(b => b.ItemCount > 0).ToList<Brand>();
                brandsModel.SelectedBrands = multipleBrands.Split('-').ToList();
                brandsModel.BreadCrumb = "";

                //brandsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, eStoreUser.CultureInfo);
            }

            return PartialView(brandsModel);
        }

        #endregion

        #region GetPropertyFilter

        [Child]
        public ActionResult GetPropertyFilter(int? id, string multipleProperties = "")
        {
            PropertyModel propertyModel = new PropertyModel();
            propertyModel.SelectedProperties = multipleProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).ToList();
            return PartialView(propertyModel);
        }

        #endregion

        #region GetPropertySelection

        [Child]
        public ActionResult GetPropertySelection(int? id)
        {
            PropertiesModel propertiesModel = new PropertiesModel();
            if (id.HasValue)
            {
                propertiesModel.CategoryId = (int)id;
                propertiesModel.Properties = dbProduct.GetPropertiesList((int)id).ToList<Property>();


                //brandsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, eStoreUser.CultureInfo);
            }

            return PartialView(propertiesModel);
        }

        #endregion

        #region GetPropertySelectionList

        [Child]
        public ActionResult GetPropertySelectionList(string aciklama, int? Kategori, int? siralama, string multipleProperties = "")
        {
            PropertyModel propertyModel = new PropertyModel();
            if (!String.IsNullOrEmpty(aciklama))
            {
                propertyModel.PropertiesAciklama = Convert.ToString(aciklama);
                IList<PropertyList> PropertyListe = dbProduct.GetPropertyList(aciklama, (int)Kategori).ToList<PropertyList>();
                IList<PropertyList> PropertyListe2 = dbProduct.GetPropertyList(aciklama, 0).ToList<PropertyList>();
                foreach (var pl in PropertyListe)
                {
                    if (!string.IsNullOrEmpty(pl.Bilgi))
                    {
                        foreach (var p in pl.Bilgi.Split(new string[] { "|||" }, StringSplitOptions.None))
                        {
                            PropertyList pln = new PropertyList();
                            pln.Aciklama = pl.Aciklama;
                            pln.Bilgi = p;
                            pln.KartId = pl.KartId;
                            if (PropertyListe2.Where(o => o.Bilgi == p).Count() <= 0)
                            {
                                PropertyListe2.Add(pln);
                            }
                        }
                    }
                }
                propertyModel.Property = PropertyListe2.OrderBy(o => o.Bilgi).ToList<PropertyList>();
                //ViewBag.MultipleProperties = multipleProperties;
                propertyModel.SelectedProperties = multipleProperties.Split(new string[] { "_-_" }, StringSplitOptions.None).ToList();
                propertyModel.siralama = (int)siralama;
            }

            return PartialView(propertyModel);
        }

        #endregion

        #region GetBestSellerPage

        [Child]
        public ActionResult GetBestSellerPage(int? id, int? brand, int? itemCount, int? totalItemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string search, int page = 1)
        {
            ViewBag.Page = page;
            var cacheKey = "GetBestSellerPage" + id + brand + itemCount + totalItemCount + minPrice + maxPrice + multipleBrands + search + page;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }

            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;

            productsModel.IsManager = eStoreUser.IsManager;
            productsModel.IsSeoManager = eStoreUser.IsSeoManager;
            productsModel.BasketLocked = eStoreUser.BasketLocked;

            var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
            if (posListe != null)
            {
                posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
            }
            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;


                productsModel.RelatedProducts = dbProduct.GetBestSellerList2((int)id, 0, (int)brand, eStoreUser.AccountId, "", (int)totalItemCount, "SATIS");
                productsModel.RelatedProducts = productsModel.RelatedProducts.OrderByDescending(o => o.BillOut).ThenByDescending(o => o.TotalViewCount).ThenByDescending(o => o.ProductId).Take((int)totalItemCount).ToArray<Product>();


                productsModel.RelatedItemCount = productsModel.RelatedProducts.Count();

                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));

                }

                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                {
                    if (minPrice == maxPrice)
                        maxPrice++;
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).Skip(PageStart).Take(PageEnd).ToArray<Product>();
                }
                else
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();
                }

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, Parameters.HomePageCategoryId, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region GetNewProductsPage

        [Child]
        public ActionResult GetNewProductsPage(int? id, int? brand, int? itemCount, int? totalItemCount, decimal? minPrice, decimal? maxPrice, string multipleBrands, string search, int page = 1)
        {
            ViewBag.Page = page;
            var cacheKey = "GetNewProductsPage" + id + brand + itemCount + totalItemCount + minPrice + maxPrice + multipleBrands + search + page;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }

            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            int PageStart = (page * (int)itemCount) - (int)itemCount;
            int PageEnd = (int)itemCount;

            productsModel.IsManager = eStoreUser.IsManager;
            productsModel.IsSeoManager = eStoreUser.IsSeoManager;
            productsModel.BasketLocked = eStoreUser.BasketLocked;

            var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
            if (posListe != null)
            {
                posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CommissionRate;
                var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit1).FirstOrDefault().CampaignCommissionRate;
                productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit1 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
            }
            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;
                if (string.IsNullOrEmpty(search)) search = "";

                productsModel.Brand = (int)brand;
                productsModel.SearchText = search;


                productsModel.RelatedProducts = dbProduct.GetBestSellerList((int)id, 0, (int)brand, eStoreUser.AccountId, "", (int)totalItemCount, "YENI").OrderByDescending(o => o.NewProduct).ThenByDescending(o => o.ProductId).ToArray<Product>();
                productsModel.RelatedItemCount = productsModel.RelatedProducts.Count();

                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));

                }

                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                {
                    if (minPrice == maxPrice)
                        maxPrice++;
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).Skip(PageStart).Take(PageEnd).ToArray<Product>();
                }
                else
                {
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Skip(PageStart).Take(PageEnd).ToArray<Product>();
                }


                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, Parameters.HomePageCategoryId, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region GetCampaignsPage

        [Child]
        public ActionResult GetCampaignsPage(int? id, int? brand, decimal? minPrice, decimal? maxPrice, int? columncount, string multipleBrands, string campaigntype = "hediye")
        {
            var cacheKey = "GetCampaignsPage" + id + brand + minPrice + maxPrice + columncount + multipleBrands + campaigntype;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                ViewBag.CampaignType = campaigntype;
                return PartialView(HttpRuntime.Cache[cacheKey] as KL.Store.Web.Models.ProductsModel);
            }

            ProductsModel productsModel = new ProductsModel();
            productsModel.Sort = 1;
            productsModel.Order = 1;
            productsModel.IsManager = eStoreUser.IsManager;
            productsModel.IsSeoManager = eStoreUser.IsSeoManager;
            productsModel.BasketLocked = eStoreUser.BasketLocked;
            productsModel.ColumnCount = columncount != null ? (int)columncount : 2;

            var posListe = dbPos.GetList().Where(o => o.PosVoucher == "S").FirstOrDefault();
            if (posListe != null)
            {
                posListe.PosInstallment = dbPos.GetInstallmentList(posListe.PosId, 1, 9999, 0, 0);
                var VCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit2).FirstOrDefault().CommissionRate;
                var VCCR = posListe.PosInstallment.Where(o => o.InstallmentNumber == Parameters.SenetTaksit2).FirstOrDefault().CampaignCommissionRate;
                productsModel.VoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit2 > 0 ? VCR : (1 + (Parameters.SenetEkUcret / 100));
                productsModel.CampaignVoucherRate = posListe.PosInstallment != null && Parameters.SenetTaksit2 > 0 && VCCR > 0 ? VCCR : productsModel.VoucherRate;
            }
            if (id.HasValue)
            {
                if (!brand.HasValue) brand = 0;

                productsModel.Brand = (int)brand;


                productsModel.RelatedProducts = dbProduct.GetCampaignsListByType((int)id, 0, (int)brand, eStoreUser.AccountId, campaigntype).OrderByDescending(o => o.ProductId).ToArray<Product>();
                productsModel.RelatedItemCount = productsModel.RelatedProducts.Count();

                if (productsModel.RelatedProducts.Count > 0)
                {
                    productsModel.MinPriceTotal = Convert.ToDecimal(productsModel.RelatedProducts.Min(o => o.Price));
                    productsModel.MaxPriceTotal = Convert.ToDecimal(productsModel.RelatedProducts.Max(o => o.Price));

                }

                ViewBag.CampaignType = campaigntype;
                productsModel.RelatedProducts = productsModel.RelatedProducts.OrderBy(o => o.ProductName).ToList();

                if (!string.IsNullOrEmpty(multipleBrands))
                {
                    var selectedBrands = multipleBrands.Split('-').ToList();
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => selectedBrands.Contains(Convert.ToString(o.ProductBrand))).ToList<Product>();

                }
                if (Convert.ToString(minPrice) != "" && Convert.ToString(maxPrice) != "" && minPrice >= 0 && maxPrice >= 1)
                {
                    if (minPrice == maxPrice)
                        maxPrice++;
                    productsModel.RelatedProducts = productsModel.RelatedProducts.Where(o => o.Price <= maxPrice && o.Price >= minPrice).ToArray<Product>();
                }


                Category category = dbCategory.Get((int)id);
                if (category != null)
                {
                    Mapper.MapDomainToViewModel(category, productsModel);
                }

                productsModel.BreadCrumb = dbCategory.GetBreadCrumb((int)id, Parameters.MainCategoryId, Parameters.HomePageCategoryId, eStoreUser.CultureInfo);
            }

            if (string.IsNullOrEmpty(productsModel.BreadCrumb)) { productsModel.BreadCrumb = "Kategori Bulunamadı"; }
            HttpRuntime.Cache.Add(cacheKey, productsModel, null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            return PartialView(productsModel);
        }

        #endregion

        #region AddProductToCategory

        public ActionResult AddProductToCategory(int? CategoryId)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            Category cd = dbCategory.Get((int)CategoryId);
            ViewBag.CategoryName = cd.CategoryName;
            ViewBag.CategoryType = cd.CategoryType;
            ViewBag.CategoryId = (int)CategoryId;
            return View();
        }

        [HttpPost]
        public ActionResult AddProductToCategory(int CategoryId, string GrupTip, IEnumerable<int> Products)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (CategoryId <= 0)
            {
                return JavaScript("kategoriSecilmemis();");
            }
            else if (string.IsNullOrEmpty(GrupTip))
            {
                return JavaScript("grupTipSecilmemis();");
            }
            else if (Products == null || Products.Count() < 1)
            {
                return JavaScript("urunSecilmemis();");
            }
            string eklenenler = "Eklenenler: <br />";
            string eklenemeyenler = "Eklenemeyenler: <br />";
            foreach (var pr in Products)
            {
                int result = dbProduct.AddProductToCategory(pr, CategoryId, GrupTip);
                ProductMeta pm = dbProduct.GetMeta(pr);
                if (result == 1)
                {
                    eklenenler += pr.ToString() + " - " + pm.ProductName + "<br />";
                }
                else
                {
                    eklenemeyenler += pr.ToString() + " - " + pm.ProductName + "<br />";
                }
            }
            return JavaScript("urunlerEklendi('" + eklenenler + eklenemeyenler + "');");
        }

        #endregion

        #region RemoveFromCategory

        public ActionResult RemoveFromCategory(int CategoryId, int ProductId)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            Product pr = dbProduct.Get((int)ProductId, eStoreUser.AccountId);
            Category cd = dbCategory.Get((int)CategoryId);
            ViewBag.CategoryName = cd.CategoryName;
            ViewBag.CategoryId = (int)CategoryId;
            ViewBag.ProductName = pr.ProductName;
            ViewBag.ProductId = pr.ProductId;
            return View();
        }

        [HttpPost]
        public ActionResult RemoveFromCategory(int CategoryId, int ProductId, string key = "")
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.RemoveFromCategory(CategoryId, ProductId);
            if (result == 1)
            {
                return JavaScript("basarili();");
            }
            else
            {
                return JavaScript("basarisiz();");
            }
        }

        #endregion
    }
}
